<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 二维码保存数据库
 */
class Qrcode extends ST_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('qrcode_mdl');
        $this->load->model('qrlog_mdl');
    }

    public function index(){
        $code_id = $this->input->get('code_id');

        if(empty($code_id)){
            redirect(site_url());
        }

        $this->db->where('code_id',$code_id);
        $res = $this->qrcode_mdl->get_one();

        if(empty($res)){
            redirect(site_url());
        }

        $this->db->where('id',$res['id']);
        $this->db->set('num','num+1',FALSE);
        $this->qrcode_mdl->update();

        //获取用户IP
        $ip = $this->input->ip_address();
        $ip_address = '';

        if($ip == '127.0.0.1' || $ip == '::1'){
            $ip_address = '本地测试';
        }else{
            $this->db->where('ip',$ip);
            $log_res = $this->qrlog_mdl->get_one();
            if(!empty($log_res)){
                $ip_address = $log_res['area'];
            }else{
                $this->load->library('ipaddress_lib');
                $ip_address = $this->ipaddress_lib->getIPLoc_QQ($ip);
            }
        }

        $data = array(
            'qr_id' => $res['id'],
            'create_time' => $_SERVER['REQUEST_TIME'],
            'ip' => $ip,
            'area' => $ip_address
        );

        $this->qrlog_mdl->create($data);
        redirect($res['content']);
    }

    public function create(){
        $post = $this->input->post();
        if(empty($post['content'])){
            show_error('参数错误！');
        }

        $data = array(
            'code_id' => md5($_SERVER['HTTP_COOKIE'].$_SERVER['REQUEST_TIME_FLOAT'].rand(1,9999)),
            'content' => $post['content'],
            'create_time' => $_SERVER['REQUEST_TIME'],
            'num' => 0
        );

        if(!empty($post['user_id'])){
            $data['user_id'] = $post['user_id'];
            $this->db->where('user_id',$post['user_id']);
        }

        $this->db->where('content',$data['content']);
        $res = $this->qrcode_mdl->get_one();

        if(!empty($res)){
            $url = base_url('/qr/images/'.$res['code_id'].'.png');
            $result = get_headers($url);
            if($result[0] == 'HTTP/1.1 404 Not Found'){
                $post_data = array(
                    'url' => site_url('qrcode/index?code_id='.$res['code_id']),
                    'name' => $res['code_id']
                );
                $this->send_post(base_url('qr/data/jsqr.php'), $post_data);
            }
            die(base_url('/qr/images/'.$res['code_id'].'.png'));
        }

        $this->db->where('code_id',$data['code_id']);
        $res = $this->qrcode_mdl->get_one();
        if(!empty($res)){
            $data['code_id'] = md5($_SERVER['REQUEST_TIME_FLOAT'].$_SERVER['HTTP_COOKIE'].rand(1,9999));
        }
        //发送post链接， 创建图片

        $post_data = array(
            'url' => site_url('qrcode/index?code_id='.$data['code_id']),
            'name' => $data['code_id']
        );
        $r = $this->send_post(base_url('qr/data/jsqr.php'), $post_data);

        if($r == 1){
            $new_id = $this->qrcode_mdl->create($data);

            if($new_id == false){
                die('创建二维码失败');
            }
        }else{
            die('创建二维码失败');
        }

        echo die(base_url('/qr/images/'.$data['code_id'].'.png'));
    }

}

/* End of file Qrcode.php */
/* Location: ./application/controllers/Qrcode.php */